﻿using CinemaManagement.EF;
using CinemaManagement.Models;
using CinemaManagement.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CinemaManagement.Controllers
{
    public class HomeController : Controller
    {
        #region index
        public ActionResult Index()
        {
            return View();
        }
        #endregion index

        #region user login
        [HttpGet]
        public ActionResult UserLogin()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UserLogin(string email, string password)
        {
            CurrentUser currentUser = UserLoginVerify(email, password);
            if (currentUser == null)
            {
                ModelState.AddModelError("failed", "用户名或者密码错误");
                return View();
            }
            else
            {
                HttpContext.Session["CurrentUser"] = currentUser;
                return base.Redirect("/User/Index");
            }
        }
        #endregion user login

        #region user register
        [HttpGet]
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                Customer newUser = new Customer()
                {
                    userName = model.Name,
                    userEmail = model.Email,
                    userPwd = model.Password,
                    userCreate = DateTime.Now,
                    userModified = DateTime.Now,
                };
                DbHelper.Instance.Db.Customer.Add(newUser);
                DbHelper.Instance.Db.SaveChanges();
                //TODO:注册逻辑
                return RedirectToAction("UserLogin", "Home");
            }
            return View(model);
        }
        #endregion user register

        #region admin login
        [HttpGet]
        public ActionResult AdminLogin()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AdminLogin(string name, string password)
        {
            CurrentUser currentUser = AdminLoginVerify(name, password);
            if (currentUser == null)
            {
                ModelState.AddModelError("failed", "管理员或者密码错误");
                return View();
            }
            else
            {
                HttpContext.Session["CurrentUser"] = currentUser;
                return base.Redirect("/Admin/Index");
            }
        }
        #endregion admin login

        #region logout
        [HttpPost]
        public ActionResult Logout()
        {
            //Cookie
            HttpCookie myCookie = HttpContext.Request.Cookies["CurrentUser"];
            if (myCookie != null)
            {
                myCookie.Expires = DateTime.Now.AddMinutes(-1);//设置过过期
                HttpContext.Response.Cookies.Add(myCookie);
            }


            //Session
            var sessionUser = HttpContext.Session["CurrentUser"];
            if (sessionUser != null && sessionUser is CurrentUser)
            {
                CurrentUser currentUser = (CurrentUser)HttpContext.Session["CurrentUser"];

            }
            HttpContext.Session["CurrentUser"] = null;//表示将制定的键的值清空，并释放掉，
            HttpContext.Session.Remove("CurrentUser");
            HttpContext.Session.Clear();//表示将会话中所有的session的键值都清空，但是session还是依然存在，
            HttpContext.Session.RemoveAll();//
            HttpContext.Session.Abandon();//就是把当前Session对象删除了，下一次就是新的Session了   

            return RedirectToAction("Index", "Home");
        }
        #endregion logout

        #region private method
        /// <summary>
        /// 用户登录验证
        /// </summary>
        /// <param name="email">邮箱</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        private CurrentUser UserLoginVerify(string email, string pwd)
        {
            CurrentUser user = null;
            Customer customer = DbHelper.Instance.Db.Customer.FirstOrDefault(o => o.userEmail == email);
            //
            if (customer != null && customer.userPwd == pwd)
            {
                user = new CurrentUser
                {
                    Id = customer.userId,
                    Name = customer.userName,
                    Account = customer.userName,
                    Email = customer.userEmail,
                    Password = customer.userPwd,
                    LoginTime = DateTime.Now,
                };
            }
            return user;
        }
        /// <summary>
        /// 管理员登录验证
        /// </summary>
        /// <param name="name">管理员</param>
        /// <param name="pwd">密码</param>
        /// <returns></returns>
        private CurrentUser AdminLoginVerify(string name, string pwd)
        {
            CurrentUser user = null;
            if (name.Equals("admin") && pwd.Equals("123456"))
            {
                user = new CurrentUser()
                {
                    Id = 007,
                    Name = name,
                    Account = "POWER",
                    Email = "123456@outlook.com",
                    Password = pwd,
                    LoginTime = DateTime.Now
                };
            }
            return user;
        }
        #endregion private method
    }
}